package com.example.covid19.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.covid19.common.Dto.CkcDto;
import com.example.covid19.common.Dto.Ckdeto2;
import com.example.covid19.common.lang.Result;
import com.example.covid19.entity.Cravate;
import com.example.covid19.mapper.CravateMapper;
import com.example.covid19.service.CravateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author BaiZhengChun
 * @since 2023-03-07
 */
@RestController
@RequestMapping("/cravate")
public class CravateController {
    @Autowired
    CravateMapper cravateMapper;
    @Autowired
    CravateService cravateService;

    @RequestMapping("/getlist")
    public Result getlist() {
        return Result.success(200, "ok", cravateMapper.getccol());
    }

    /**
     *
     * @param cid
     * @param cravateName
     * @return 添加系部
     */
    @RequestMapping("addCravate")
    public Result addCravate(@RequestParam("cid")Integer cid,@RequestParam("cravateName")String cravateName){
        Cravate cravate = new Cravate();
        cravate.setCid(cid);
        cravate.setRname(cravateName);
        boolean flag = cravateService.save(cravate);
        if (flag){
            return Result.success(200,"ok",null);
        }else {
            return Result.fail(401,"error",null);
        }
    }

    /**
     *
     * @param xid
     * @param cravateName
     * @return 修改系部
     */
    @RequestMapping("EditCravate")
    public Result EditCravate(@RequestParam("xid")Integer xid,@RequestParam("cravateName") String cravateName){
        Cravate cravate = cravateService.getOne(new QueryWrapper<Cravate>().eq("xid",xid));
        cravate.setRname(cravateName);
        boolean flag = cravateService.updateById(cravate);
        if (flag){
            return Result.success(200,"ok",null);
        }else {
            return Result.fail(401,"ok",null);
        }
    }

    /**
     *
     * @param id
     * @return检查是否可以进行删除
     */
    @RequestMapping("isdeldete")
    public Result isdeldete(@RequestParam("id")Integer id){
        List<Ckdeto2> ckcDtoList = cravateMapper.candelete(id);
        if (ckcDtoList != null && ckcDtoList.size() > 0 && ckcDtoList.get(0) == null){
            return Result.success(200,"ok",true);
        }else {
            return Result.success(200, "ok", false);
        }
    }

    /**
     *
     * @param id
     * @return 删除系部
     */
    @RequestMapping("deleteCrvate")
    public Result deleteCrvate(@RequestParam("id")Integer id){
        boolean flag = cravateService.removeById(id);
        if (flag){
            return Result.success(200,"ok",null);
        }else {
            return Result.fail(401,"error",null);
        }
    }

}
